package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.UserIcardInfo;

public class UserIcardInfoRowMapper implements RowMapper<UserIcardInfo> {

    @SuppressWarnings("unused")
    private int type;
    
    public static final int AND_CARDNUM=1;

    public UserIcardInfoRowMapper() {
    }

    public UserIcardInfoRowMapper(int type) {
        this.type = type;
    }

    @Override
    public UserIcardInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
        
        UserIcardInfo uii = parseUserIcard(new UserIcardInfo(), rs);
        uii.setCdate_str(SystemDateFormatUtil.formatDate(uii.getCdate(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));
        if(uii.getType()==(short)1){
            uii.setType_str("进校园");
        }else{
            uii.setType_str("出校园");
        }
        
        switch (type) {
            case AND_CARDNUM:
                uii.setCardnum(rs.getString("cardnum"));
                uii.setName_student(rs.getString("name_student"));
                break;
        }
        return uii;
    }

    public static UserIcardInfo parseUserIcard(UserIcardInfo ui, ResultSet rs) throws SQLException {
        return parseUserIcard(ui, rs, null);
    }

    public static UserIcardInfo parseUserIcard(UserIcardInfo ui, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }

        try {
            ui.setUeid(rs.getLong(su+"ueid"));
        } catch (SQLException e) {
            ui.setUeid(null);
        }
        try {
            ui.setUid(rs.getLong(su+"uid"));
        } catch (SQLException e) {
            ui.setUid(null);
        }
        try {
            ui.setIcid(rs.getLong(su+"icid"));
        } catch (SQLException e) {
            ui.setIcid(null);
        }
        try {
            ui.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            ui.setCdate(null);
        }
        try {
            ui.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            ui.setType(null);
        }
        try {
            ui.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            ui.setImg(null);
        }
        
        
        return ui;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, UserIcardInfo ui) {
        par.put("ueid", ui.getUeid());
        par.put("uid", ui.getUid());
        par.put("icid", ui.getIcid());
        par.put("cdate", ui.getCdate());
        par.put("type",ui.getType());
        par.put("img",ui.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, UserIcardInfo ui) {
        args.put("ueid", ui.getUeid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final UserIcardInfo ui) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(ui),
                        new String[] { "ueid" });
                parsePS(ps, ui);
                return ps;
            }
        };
    }

    private static String parseSQL(UserIcardInfo ui) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into user_icard_info(");
        int count = 0;
        if (null != ui.getUid()) {
            sql.append("uid,");
            count++;
        }

        if (null != ui.getIcid()) {
            sql.append("icid,");
            count++;
        }

        if (null != ui.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        if (null != ui.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != ui.getImg()) {
            sql.append("img,");
            count++;
        }
    
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, UserIcardInfo ui)
        throws SQLException {
        int count = 1;
        if (null != ui.getUid()) {
            ps.setLong(count++,ui.getUid());
        }
        
        if (null != ui.getIcid()) {
            ps.setLong(count++, ui.getIcid());
        }


        if (null != ui.getCdate()) {
            ps.setTimestamp(count++, ui.getCdate());
        }
        if (null != ui.getType()) {
            ps.setShort(count++, ui.getType());
        }
        if (null != ui.getImg()) {
            ps.setString(count++, ui.getImg());
        }
    

    }

}
